package com.suning.sawp.service.branch;

import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimaps;
import com.google.common.collect.Sets;
import com.suning.sawp.dto.storeman.BranchInfo;
import com.suning.sawp.dto.storeman.Category;

public abstract class BranchRetStatisServiceBase {

    /**
     * 
     */
    protected <T1 extends BiStoreBranch4RetGds, T2 extends BiStoreRetGdBranch429> BranchRetStatis indexBranchRetGds(
            List<T1> branch4s, List<T2> branch429) {
        // .<Category, T>
        class Index {
            Set<Category> categories = Sets.newLinkedHashSet();

            void index(BiStoreRetGdBranch429 input) {
                Category category = new Category();
                category.setCategoryCode(input.getCateCode());
                category.setCategoryName(input.getCateName());
                categories.add(category);
            }
        }
        final Index index = new Index();
        final Map<String, List<T2>> map429 = Maps
                .newHashMap(Multimaps.asMap(Multimaps.index(branch429, new Function<BiStoreRetGdBranch429, String>() {
                    @Override
                    public String apply(BiStoreRetGdBranch429 input) {
                        index.index(input);
                        return input.getBranch4code();
                    }
                })));
        BranchRetStatis branchRetStatis = new BranchRetStatis();
        branchRetStatis.setBranches(
                Lists.newArrayList(Lists.transform(branch4s, new Function<BiStoreBranch4RetGds, BranchInfo>() {
                    @Override
                    public BranchInfo apply(BiStoreBranch4RetGds input) {
                        BranchInfo branchInfo = new BranchInfo();
                        branchInfo.setBranch4code(input.getBranch4code());
                        List<T2> branch9;
                        if (null != (branch9 = map429.get(input.getBranch4code()))) {
                            branchInfo.setBranch9codes(
                                    Lists.newArrayList(Lists.transform(branch9, new Function<T2, String>() {
                                        @Override
                                        public String apply(T2 input) {
                                            return input.getBranch9code();
                                        }
                                    })));
                        }
                        branchInfo.setBranchName(input.getBranchName());
                        return branchInfo;
                    }
                })));
        //branchRetStatis.setCategories(Lists.newArrayList(index.categories));
        return branchRetStatis;
    }

}
